//
//  LoginViewController.m
//  citzenWorkStation
//
//  Created by yang qing on 2018/2/12.
//  Copyright © 2018年 yang qing. All rights reserved.
//

#import "LoginViewController.h"
#import "UIColor+Hex.h"
#import "AFNetworking.h"
#import "API.h"
#import "RequestManage.h"
#import "NSObject+YYModel.h"
#import "UserInfoBean.h"
#import "DataSaveUtils.h"
#import "CommonReqManage.h"
#import "RegisterViewController.h"
#import "ForgetPswViewController.h"
@interface LoginViewController ()
@property (weak, nonatomic) IBOutlet UIButton *btnBack;
@property (weak, nonatomic) IBOutlet UITextField *tfUsername;
@property (weak, nonatomic) IBOutlet UITextField *tfPassword;
@property (weak, nonatomic) IBOutlet UIButton *btnLogin;
@property (weak, nonatomic) IBOutlet UIButton *btnForget;
@property (weak, nonatomic) IBOutlet UIButton *btnRegister;
@property (weak, nonatomic) IBOutlet UIView *titleView;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initNav];
     [self initView];
    [self initListener];
    
}

- (void)initNav{
    self.navigationController.navigationBar.barTintColor =
    [UIColor colorWithHexString:ThemeColor];
    //设置导航栏标题文字和文字大小
    [self setNavtitle:@"登录"];
    //设置两侧按钮
    [self setNavLeftImg:@"nav_back"];
}
- (void)setNavtitle:(NSString *)title{
    self.navigationItem.title = title;
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont systemFontOfSize:20]}];
}
- (void)setNavLeftImg:(NSString *)name{
    UIButton *menuButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [menuButton setFrame:CGRectMake(-20.0, 0.0, 44.0, 44.0)];
    [menuButton addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    [menuButton setImage:[UIImage imageNamed:name] forState:UIControlStateNormal];
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 44.0, 44.0)];
    [view addSubview:menuButton];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:view];
    self.navigationItem.leftBarButtonItem = leftItem;
}
- (void)initView{
    
    self.tfUsername.text=@"18600887431";
    self.tfPassword.text = @"111111";
//    self.navigationController.navigationBar.hidden = YES;
    
    //设置输入框输入样式
    _tfUsername.borderStyle = UITextBorderStyleNone;
    _tfUsername.keyboardType = UIKeyboardTypePhonePad;
    _tfPassword.borderStyle = UITextBorderStyleNone;
    //设置清除按钮在编辑时候显示
    _tfUsername.clearButtonMode = UITextFieldViewModeWhileEditing;
    _tfPassword.clearButtonMode = UITextFieldViewModeWhileEditing;
    //设置密码输入框密码不可见
    _tfPassword.secureTextEntry=YES;
    //设置按钮样式
    _btnLogin.layer.cornerRadius = 8.0;
}

- (void)initListener{
    [self.btnBack addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    
    [self.btnLogin addTarget:self action:@selector(loginClick) forControlEvents:UIControlEventTouchUpInside];
    
    [self.btnForget addTarget:self action:@selector(forgetPswClick) forControlEvents:UIControlEventTouchUpInside];
    
    [self.btnRegister addTarget:self action:@selector(registerClick) forControlEvents:UIControlEventTouchUpInside];
}

- (void)loginClick{
    //检查数据完整性
    if([self checkData]){
        NSString *loginName = _tfUsername.text;
        NSString *password = _tfPassword.text;
        NSString *phonemodel = [[UIDevice currentDevice] model];
        NSString *systemversion = [[UIDevice currentDevice] systemVersion];
        NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
        NSString *appversion = [infoDic objectForKey:@"CFBundleShortVersionString"];
        NSString *operatesystem = @"IOS";
    
        NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
        [params setObject:loginName forKey:@"loginName"];
        [params setObject:password forKey:@"password"];
        [params setObject:phonemodel forKey:@"phonemodel"];
        [params setObject:systemversion forKey:@"systemversion"];
        [params setObject:appversion forKey:@"appversion"];
        [params setObject:@"" forKey:@"is_login"];
        [params setObject:@"" forKey:@"token"];
        [params setObject:operatesystem forKey:@"operatesystem"];
        
       
        [self showHub:@""];
        [CommonReqManage LoginWidthParam:params success:^(id success) {
            [self hiddenHub];
            NSString *status =  success[@"status"];
            if([status isEqualToString:@"1"]){
                
                UserInfoBean *userBean =  [UserInfoBean yy_modelWithJSON:success[@"result"]];
                //保存用户信息
                userBean.password = _tfPassword.text;
                [DataSaveUtils saveUserInfo:userBean];
//                //保存token
//                [DataSaveUtils saveToken:userBean.token];
                [self showToast:@"登录成功"];
                [self backClick];
            }else{
                [self showTip:success[@"message"] cancelText:@"" confirmText:@"确定" Confirm:nil Cancel:nil];
                
            }
        } failed:^(id failure) {
             [self hiddenHub];
            [self showHubAuto:@"请求失败"];
        }];
    }
}

- (void)backClick{
    [self dismissViewControllerAnimated:YES completion:nil];
}


- (void)forgetPswClick{
    ForgetPswViewController *forgetPwd = [[ForgetPswViewController alloc]init];
    forgetPwd.titleName = @"忘记密码";
    [self.navigationController pushViewController:forgetPwd animated:YES];
}

NSString *ss;
- (void)registerClick{
    RegisterViewController *registerVc = [[RegisterViewController alloc] init];
    [self.navigationController pushViewController:registerVc animated:YES];
}

- (Boolean)checkData{
    if([_tfUsername.text isEqualToString:@""]){
        [self showHubAuto:@"用户名不能为空" image:@"icon_warn"];
        return NO;
    }
    if([_tfPassword.text isEqualToString:@""]){
        [self showHubAuto:@"密码不能为空" image:@"icon_warn"];
        return NO;
    }
    return YES;
}


@end
